﻿Imports System.IO

Namespace EyeOpen.IO

	Public Class DirectorySearch
		Public Function Search(ByVal directory As IDirectoryInfo, ByVal fileName As String, ByVal logger As ILogger) As FileInfo
			Try
				Dim file = directory.GetFiles(fileName).SingleOrDefault()

				If file IsNot Nothing Then
					Return file
				End If

				Dim directories = directory.GetDirectories()

				If directories.Count() = 0 Then
					Return Nothing
				End If

				For Each child In directories
					logger.Trace(child.FullName)

					Dim fileFound = Search(child, fileName, logger)

					If fileFound IsNot Nothing Then
						Return fileFound
					End If
				Next child
			Catch ex As Exception
				logger.Exception(ex)
				Return Nothing
			End Try

			Return Nothing
		End Function
	End Class
End Namespace